package com.gxuwz.zlc.controller;

import com.gxuwz.zlc.common.Result;
import com.gxuwz.zlc.entity.Department;
import com.gxuwz.zlc.entity.Lab;
import com.gxuwz.zlc.service.LabService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 实验室
 */
@Controller
@RequestMapping("/lab")
public class LabController {

    @Autowired
    public LabService labService;

    // 查询所有
    @RequestMapping("/findAll")
    @ResponseBody
    public List<Lab> findLab(){
        List<Lab> labList = labService.findLab();
        return labList;
    }

    // 查询一个
    @GetMapping("/findOne/{labId}")
    @ResponseBody
    public Result get(@PathVariable String labId){
        Lab labById = labService.findLabById(labId);
        // code=0,msg=成功
        return Result.success(labById);
    }

    // 添加
    @RequestMapping("/add")
    @ResponseBody
    public Map addLab(@RequestBody Lab lab){
        Map<String,String> map = new HashMap<>();
        try {
            // 判断该实验室死否存在
            List<Lab> labByLabName = labService.findLabByLabName(lab.getLabName());
            if (CollectionUtils.isEmpty(labByLabName)){
                labService.addLab(lab);
                map.put("code","200");
                map.put("msg","信息添加成功");
            }else {
                map.put("code","500");
                map.put("msg","该实验室已存在");
            }
        } catch (Exception e) {
            map.put("code","500");
            map.put("msg","添加失败");
            e.printStackTrace();
        }
        return map;
    }

    // 修改
    @RequestMapping("/update")
    @ResponseBody
    public Map updateLab(@RequestBody Lab lab){
        labService.updateLab(lab);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("msg","修改成功");
        return map;
    }

    // 删除
    @RequestMapping("/delete")
    @ResponseBody
    public Map deleteLab(@RequestParam(name = "labId") String labId){
        labService.deleteLab(labId);
        Map<String,String> map = new HashMap<>();
        map.put("code","200");
        map.put("mag","删除成功");
        return map;
    }

}
